import { useFetch } from '../../../Scripts/hooks/useFetch.js'
const { ElMessage, ElMessageBox } = ElementPlus

export function useMESFeedback() {
  const getMESFeedBackData = async configObj => {

    ElMessageBox.confirm(`即将跳转并打开【问题快反提报】页面，是否继续?`, '确认', { type: 'warning' }).then(async () => {
      const {
        feedBackDisabled,
        selectRows,
        resourceList,
        containerInfo,
        currentSelectContainer,
        url,
        method,
        employeename
      } = configObj;

      if (feedBackDisabled) {
        return ElMessage({ type: 'error', message: `用户不存在，不能提交问题快反`, grouping: true });
      }

      // 未勾选批次时，直接跳转至问题快反提报url
      if (!containerInfo.containertype || (containerInfo.containertype === 7 && selectRows.length === 0)) {
        window.open(`http://10.15.7.194:8088/portal/ssologin_mes_feedback_open.jsp?user=${configObj.employeename}`);
        return;
      }

      const { togetherflag, productname, workflowstepname, specno, containertype } = containerInfo;
      let containerid = (containertype === 7 ? selectRows[0].containerid : currentSelectContainer.containerid);
      const resourceid = resourceList.value[0].resourceid;
      const resourcename = resourceList.value.filter(i => i.resourceid === resourceid)[0].resourcename;


      await useFetch({
        url: `${url}?actions=${method}`,
        data: {
          type: `${method}`,
          createuser: `MES_${employeename}`,
          containerid,
          productname,
          togetherflag,
          workflowstepname,
          specno,
          resourcename
        }
      });
      window.open(`http://10.15.7.194:8088/portal/ssologin_mes_feedback_open.jsp?user=${employeename}`);
    }).catch(() => {
      ElMessage({ type: 'info', message: '已取消' });
    });
  };

  return { getMESFeedBackData };
}